<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>previewform.cpp 示例文件 | Qt Widgets 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="qtwidgets-tools-codecs-example.html">Text Codecs Example</a></td><td >previewform.cpp 示例文件</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">previewform.cpp 示例文件</h1>
<span class="subtitle">tools/codecs/previewform.cpp</span>
<!-- $$$tools/codecs/previewform.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp">

  <span class="comment">/****************************************************************************
  **
  ** Copyright (C) 2016 The Qt Company Ltd.
  ** Contact: https://www.qt.io/licensing/
  **
  ** This file is part of the examples of the Qt Toolkit.
  **
  ** $QT_BEGIN_LICENSE:BSD$
  ** Commercial License Usage
  ** Licensees holding valid commercial Qt licenses may use this file in
  ** accordance with the commercial license agreement provided with the
  ** Software or, alternatively, in accordance with the terms contained in
  ** a written agreement between you and The Qt Company. For licensing terms
  ** and conditions see https://www.qt.io/terms-conditions. For further
  ** information use the contact form at https://www.qt.io/contact-us.
  **
  ** BSD License Usage
  ** Alternatively, you may use this file under the terms of the BSD license
  ** as follows:
  **
  ** &quot;Redistribution and use in source and binary forms, with or without
  ** modification, are permitted provided that the following conditions are
  ** met:
  **   * Redistributions of source code must retain the above copyright
  **     notice, this list of conditions and the following disclaimer.
  **   * Redistributions in binary form must reproduce the above copyright
  **     notice, this list of conditions and the following disclaimer in
  **     the documentation and/or other materials provided with the
  **     distribution.
  **   * Neither the name of The Qt Company Ltd nor the names of its
  **     contributors may be used to endorse or promote products derived
  **     from this software without specific prior written permission.
  **
  **
  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
  **
  ** $QT_END_LICENSE$
  **
  ****************************************************************************/</span>

  <span class="preprocessor">#include &lt;QtWidgets&gt;</span>

  <span class="preprocessor">#include &quot;previewform.h&quot;</span>

  <span class="comment">// Helpers for creating hex dumps</span>
  <span class="keyword">static</span> <span class="type">void</span> indent(<span class="type"><a href="../qtcore/qtextstream.html">QTextStream</a></span> <span class="operator">&amp;</span>str<span class="operator">,</span> <span class="type">int</span> indent)
  {
      <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span> indent; <span class="operator">+</span><span class="operator">+</span>i)
          str <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="char">' '</span>;
  }

  <span class="keyword">static</span> <span class="type">void</span> formatHex(<span class="type"><a href="../qtcore/qtextstream.html">QTextStream</a></span> <span class="operator">&amp;</span>str<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span> <span class="operator">&amp;</span>data)
  {
      <span class="keyword">const</span> <span class="type">int</span> fieldWidth <span class="operator">=</span> str<span class="operator">.</span>fieldWidth();
      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qtextstream.html">QTextStream</a></span><span class="operator">::</span>FieldAlignment alignment <span class="operator">=</span> str<span class="operator">.</span>fieldAlignment();
      <span class="keyword">const</span> <span class="type">int</span> base <span class="operator">=</span> str<span class="operator">.</span>integerBase();
      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qchar.html">QChar</a></span> padChar <span class="operator">=</span> str<span class="operator">.</span>padChar();
      str<span class="operator">.</span>setIntegerBase(<span class="number">16</span>);
      str<span class="operator">.</span>setPadChar(QLatin1Char(<span class="char">'0'</span>));
      str<span class="operator">.</span>setFieldAlignment(<span class="type"><a href="../qtcore/qtextstream.html">QTextStream</a></span><span class="operator">::</span>AlignRight);

      <span class="keyword">const</span> <span class="type">unsigned</span> <span class="type">char</span> <span class="operator">*</span>p <span class="operator">=</span> <span class="keyword">reinterpret_cast</span><span class="operator">&lt;</span><span class="keyword">const</span> <span class="type">unsigned</span> <span class="type">char</span> <span class="operator">*</span><span class="operator">&gt;</span>(data<span class="operator">.</span>constBegin());
      <span class="keyword">for</span> (<span class="keyword">const</span> <span class="type">unsigned</span> <span class="type">char</span> <span class="operator">*</span>end <span class="operator">=</span> p <span class="operator">+</span> data<span class="operator">.</span>size(); p <span class="operator">&lt;</span> end; <span class="operator">+</span><span class="operator">+</span>p) {
          str <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="char">' '</span>;
          str<span class="operator">.</span>setFieldWidth(<span class="number">2</span>);
          str <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="type">unsigned</span>(<span class="operator">*</span>p);
          str<span class="operator">.</span>setFieldWidth(fieldWidth);
      }
      str<span class="operator">.</span>setFieldAlignment(alignment);
      str<span class="operator">.</span>setPadChar(padChar);
      str<span class="operator">.</span>setIntegerBase(base);
  }

  <span class="keyword">static</span> <span class="type">void</span> formatPrintableCharacters(<span class="type"><a href="../qtcore/qtextstream.html">QTextStream</a></span> <span class="operator">&amp;</span>str<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span> <span class="operator">&amp;</span>data)
  {
      <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span><span class="operator">,</span> size <span class="operator">=</span> data<span class="operator">.</span>size(); i <span class="operator">&lt;</span> size; <span class="operator">+</span><span class="operator">+</span>i) {
          <span class="keyword">const</span> <span class="type">char</span> c <span class="operator">=</span> data<span class="operator">.</span>at(i);
          <span class="keyword">switch</span> (c) {
          <span class="keyword">case</span> <span class="char">'\0'</span>:
              str <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\0&quot;</span>;
              <span class="keyword">break</span>;
          <span class="keyword">case</span> <span class="char">'\t'</span>:
              str <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\t&quot;</span>;
              <span class="keyword">break</span>;
          <span class="keyword">case</span> <span class="char">'\r'</span>:
              str <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\r&quot;</span>;
              <span class="keyword">break</span>;
          <span class="keyword">case</span> <span class="char">'\n'</span>:
              str <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;\\n&quot;</span>;
              <span class="keyword">break</span>;
          <span class="keyword">default</span>:
              <span class="keyword">if</span> (c <span class="operator">&gt;</span><span class="operator">=</span> <span class="number">32</span> <span class="operator">&amp;</span><span class="operator">&amp;</span> <span class="type"><a href="../qtcore/qtglobal.html#uchar-typedef">uchar</a></span>(c) <span class="operator">&lt;</span> <span class="number">127</span>)
                  str <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="char">' '</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> c;
              <span class="keyword">else</span>
                  str <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;..&quot;</span>;
              <span class="keyword">break</span>;
          }
      }
  }

  <span class="keyword">static</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> formatHexDump(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span> <span class="operator">&amp;</span>data)
  {
      <span class="keyword">enum</span> { lineWidth <span class="operator">=</span> <span class="number">16</span> };
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> result;
      <span class="type"><a href="../qtcore/qtextstream.html">QTextStream</a></span> str(<span class="operator">&amp;</span>result);
      str<span class="operator">.</span>setIntegerBase(<span class="number">16</span>);
      str<span class="operator">.</span>setPadChar(QLatin1Char(<span class="char">'0'</span>));
      <span class="keyword">const</span> <span class="type">int</span> fieldWidth <span class="operator">=</span> str<span class="operator">.</span>fieldWidth();
      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qtextstream.html">QTextStream</a></span><span class="operator">::</span>FieldAlignment alignment <span class="operator">=</span> str<span class="operator">.</span>fieldAlignment();
      <span class="keyword">for</span> (<span class="type">int</span> a <span class="operator">=</span> <span class="number">0</span><span class="operator">,</span> size <span class="operator">=</span> data<span class="operator">.</span>size(); a <span class="operator">&lt;</span> size; a <span class="operator">+</span><span class="operator">=</span> lineWidth) {
          str<span class="operator">.</span>setFieldAlignment(<span class="type"><a href="../qtcore/qtextstream.html">QTextStream</a></span><span class="operator">::</span>AlignRight);
          str<span class="operator">.</span>setFieldWidth(<span class="number">8</span>);
          str <span class="operator">&lt;</span><span class="operator">&lt;</span> a;
          str<span class="operator">.</span>setFieldWidth(fieldWidth);
          str<span class="operator">.</span>setFieldAlignment(alignment);

          <span class="keyword">const</span> <span class="type">int</span> end <span class="operator">=</span> <a href="../qtcore/qtglobal.html#qMin">qMin</a>(a <span class="operator">+</span> lineWidth<span class="operator">,</span> size);
          <span class="keyword">const</span> <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span> line <span class="operator">=</span> data<span class="operator">.</span>mid(a<span class="operator">,</span> end <span class="operator">-</span> a);

          formatHex(str<span class="operator">,</span> line);
          indent(str<span class="operator">,</span> <span class="number">3</span> <span class="operator">*</span> (lineWidth <span class="operator">-</span> line<span class="operator">.</span>size()));

          str <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="char">' '</span>;
          formatPrintableCharacters(str<span class="operator">,</span> line);
          indent(str<span class="operator">,</span> <span class="number">2</span> <span class="operator">*</span> (lineWidth <span class="operator">-</span> line<span class="operator">.</span>size()));
          str <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="char">'\n'</span>;
      }
      <span class="keyword">return</span> result;
  }

  PreviewForm<span class="operator">::</span>PreviewForm(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
      : <span class="type"><a href="qdialog.html">QDialog</a></span>(parent)
  {
      setWindowFlags(windowFlags() <span class="operator">&amp;</span> <span class="operator">~</span><span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>WindowContextHelpButtonHint);
      encodingComboBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qcombobox.html">QComboBox</a></span>;

      <span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>encodingLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">&quot;&amp;Encoding:&quot;</span>));
      encodingLabel<span class="operator">-</span><span class="operator">&gt;</span>setBuddy(encodingComboBox);

      textEdit <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qplaintextedit.html">QPlainTextEdit</a></span>;
      textEdit<span class="operator">-</span><span class="operator">&gt;</span>setLineWrapMode(<span class="type"><a href="qplaintextedit.html">QPlainTextEdit</a></span><span class="operator">::</span>NoWrap);
      textEdit<span class="operator">-</span><span class="operator">&gt;</span>setReadOnly(<span class="keyword">true</span>);
      hexDumpEdit <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qplaintextedit.html">QPlainTextEdit</a></span>;
      hexDumpEdit<span class="operator">-</span><span class="operator">&gt;</span>setLineWrapMode(<span class="type"><a href="qplaintextedit.html">QPlainTextEdit</a></span><span class="operator">::</span>NoWrap);
      hexDumpEdit<span class="operator">-</span><span class="operator">&gt;</span>setReadOnly(<span class="keyword">true</span>);
      hexDumpEdit<span class="operator">-</span><span class="operator">&gt;</span>setFont(<span class="type"><a href="../qtgui/qfontdatabase.html">QFontDatabase</a></span><span class="operator">::</span>systemFont(<span class="type"><a href="../qtgui/qfontdatabase.html">QFontDatabase</a></span><span class="operator">::</span>FixedFont));

      <span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span> <span class="operator">*</span>buttonBox <span class="operator">=</span>
          <span class="keyword">new</span> <span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span>(<span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span><span class="operator">::</span>Ok <span class="operator">|</span> <span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span><span class="operator">::</span>Cancel);
      okButton <span class="operator">=</span> buttonBox<span class="operator">-</span><span class="operator">&gt;</span>button(<span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span><span class="operator">::</span>Ok);

      connect(encodingComboBox<span class="operator">,</span> <span class="type">QOverload</span><span class="operator">&lt;</span><span class="type">int</span><span class="operator">&gt;</span><span class="operator">::</span>of(<span class="operator">&amp;</span><span class="type"><a href="qcombobox.html">QComboBox</a></span><span class="operator">::</span>activated)<span class="operator">,</span>
              <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>PreviewForm<span class="operator">::</span>updateTextEdit);
      connect(buttonBox<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span><span class="operator">::</span>accepted<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qdialog.html">QDialog</a></span><span class="operator">::</span>accept);
      connect(buttonBox<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qdialogbuttonbox.html">QDialogButtonBox</a></span><span class="operator">::</span>rejected<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qdialog.html">QDialog</a></span><span class="operator">::</span>reject);

      <span class="type"><a href="qgridlayout.html">QGridLayout</a></span> <span class="operator">*</span>mainLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgridlayout.html">QGridLayout</a></span>(<span class="keyword">this</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(encodingLabel<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(encodingComboBox<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span>);
      tabWidget <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtabwidget.html">QTabWidget</a></span>;
      tabWidget<span class="operator">-</span><span class="operator">&gt;</span>addTab(textEdit<span class="operator">,</span> tr(<span class="string">&quot;Preview&quot;</span>));
      tabWidget<span class="operator">-</span><span class="operator">&gt;</span>addTab(hexDumpEdit<span class="operator">,</span> tr(<span class="string">&quot;Hex Dump&quot;</span>));
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(tabWidget<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">2</span>);
      statusLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>;
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(statusLabel<span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">2</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(buttonBox<span class="operator">,</span> <span class="number">3</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">2</span>);

      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qrect.html">QRect</a></span> screenGeometry <span class="operator">=</span> <span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>desktop()<span class="operator">-</span><span class="operator">&gt;</span>screenGeometry(<span class="keyword">this</span>);
      resize(screenGeometry<span class="operator">.</span>width() <span class="operator">*</span> <span class="number">2</span> <span class="operator">/</span> <span class="number">5</span><span class="operator">,</span> screenGeometry<span class="operator">.</span>height() <span class="operator">/</span> <span class="number">2</span>);
  }

  <span class="type">void</span> PreviewForm<span class="operator">::</span>setCodecList(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type"><a href="../qtcore/qtextcodec.html">QTextCodec</a></span> <span class="operator">*</span><span class="operator">&gt;</span> <span class="operator">&amp;</span>list)
  {
      encodingComboBox<span class="operator">-</span><span class="operator">&gt;</span>clear();
      foreach (<span class="keyword">const</span> <span class="type"><a href="../qtcore/qtextcodec.html">QTextCodec</a></span> <span class="operator">*</span>codec<span class="operator">,</span> list) {
          encodingComboBox<span class="operator">-</span><span class="operator">&gt;</span>addItem(QLatin1String(codec<span class="operator">-</span><span class="operator">&gt;</span>name())<span class="operator">,</span>
                                    <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span>(codec<span class="operator">-</span><span class="operator">&gt;</span>mibEnum()));
      }
  }

  <span class="type">void</span> PreviewForm<span class="operator">::</span>reset()
  {
      decodedStr<span class="operator">.</span>clear();
      textEdit<span class="operator">-</span><span class="operator">&gt;</span>clear();
      hexDumpEdit<span class="operator">-</span><span class="operator">&gt;</span>clear();
      statusLabel<span class="operator">-</span><span class="operator">&gt;</span>clear();
      statusLabel<span class="operator">-</span><span class="operator">&gt;</span>setStyleSheet(<span class="type"><a href="../qtcore/qstring.html">QString</a></span>());
      okButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);
      tabWidget<span class="operator">-</span><span class="operator">&gt;</span>setCurrentIndex(<span class="number">0</span>);
  }

  <span class="type">void</span> PreviewForm<span class="operator">::</span>setEncodedData(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span> <span class="operator">&amp;</span>data)
  {
      reset();
      encodedData <span class="operator">=</span> data;
      hexDumpEdit<span class="operator">-</span><span class="operator">&gt;</span>setPlainText(formatHexDump(data));
      updateTextEdit();
  }

  <span class="type">void</span> PreviewForm<span class="operator">::</span>updateTextEdit()
  {
      <span class="type">int</span> mib <span class="operator">=</span> encodingComboBox<span class="operator">-</span><span class="operator">&gt;</span>itemData(
                        encodingComboBox<span class="operator">-</span><span class="operator">&gt;</span>currentIndex())<span class="operator">.</span>toInt();
      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qtextcodec.html">QTextCodec</a></span> <span class="operator">*</span>codec <span class="operator">=</span> <span class="type"><a href="../qtcore/qtextcodec.html">QTextCodec</a></span><span class="operator">::</span>codecForMib(mib);
      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> name <span class="operator">=</span> QLatin1String(codec<span class="operator">-</span><span class="operator">&gt;</span>name());

      <span class="type"><a href="../qtcore/qtextcodec.html">QTextCodec</a></span><span class="operator">::</span>ConverterState state;
      decodedStr <span class="operator">=</span> codec<span class="operator">-</span><span class="operator">&gt;</span>toUnicode(encodedData<span class="operator">.</span>constData()<span class="operator">,</span> encodedData<span class="operator">.</span>size()<span class="operator">,</span> <span class="operator">&amp;</span>state);

      bool success <span class="operator">=</span> <span class="keyword">true</span>;
      <span class="keyword">if</span> (state<span class="operator">.</span>remainingChars) {
          success <span class="operator">=</span> <span class="keyword">false</span>;
          <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> message <span class="operator">=</span>
              tr(<span class="string">&quot;%1: conversion error at character %2&quot;</span>)
              <span class="operator">.</span>arg(name)<span class="operator">.</span>arg(encodedData<span class="operator">.</span>size() <span class="operator">-</span> state<span class="operator">.</span>remainingChars <span class="operator">+</span> <span class="number">1</span>);
          statusLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(message);
          statusLabel<span class="operator">-</span><span class="operator">&gt;</span>setStyleSheet(<span class="type"><a href="../qtcore/qstring.html#QStringLiteral">QStringLiteral</a></span>(<span class="string">&quot;background-color: \&quot;red\&quot;;&quot;</span>));
      } <span class="keyword">else</span> <span class="keyword">if</span> (state<span class="operator">.</span>invalidChars) {
          statusLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;%1: %n invalid characters&quot;</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> state<span class="operator">.</span>invalidChars)<span class="operator">.</span>arg(name));
          statusLabel<span class="operator">-</span><span class="operator">&gt;</span>setStyleSheet(<span class="type"><a href="../qtcore/qstring.html#QStringLiteral">QStringLiteral</a></span>(<span class="string">&quot;background-color: \&quot;yellow\&quot;;&quot;</span>));
      } <span class="keyword">else</span> {
          statusLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;%1: %n bytes converted&quot;</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> encodedData<span class="operator">.</span>size())<span class="operator">.</span>arg(name));
          statusLabel<span class="operator">-</span><span class="operator">&gt;</span>setStyleSheet(<span class="type"><a href="../qtcore/qstring.html">QString</a></span>());
      }
      <span class="keyword">if</span> (success)
          textEdit<span class="operator">-</span><span class="operator">&gt;</span>setPlainText(decodedStr);
      <span class="keyword">else</span>
          textEdit<span class="operator">-</span><span class="operator">&gt;</span>clear();
      okButton<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(success);
  }

</pre>
</div>
<!-- @@@tools/codecs/previewform.cpp -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
